package me.aartikov.flowscreenswitchersample.ui;


import me.aartikov.alligator.ImageTint;
import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.Screen;
import me.aartikov.alligator.annotations.RegisterScreen;
import me.aartikov.alligator.helpers.MiFraction;
import me.aartikov.alligator.helpers.FractionStack;
import me.aartikov.alligator.util.Log;
import me.aartikov.flowscreenswitchersample.ResourceTable;
import me.aartikov.flowscreenswitchersample.SampleApplication;
import me.aartikov.flowscreenswitchersample.screens.TabScreen;
import me.aartikov.flowscreenswitchersample.screens.TestFlowScreen;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@RegisterScreen(TestFlowScreen.class)
public class TestFlowMiFraction extends MiFraction implements ContainerIdProvider, Component.ClickedListener {

    private Image icon1;
    private Image icon2;
    private Image icon3;
    private Text textIcon1;
    private Text textIcon2;
    private Text textIcon3;
    private int nowSelect = ResourceTable.Id_openharmony;

    private Navigator mNavigator = SampleApplication.getNavigator();

    private Map<Integer, Screen> mTabScreenMap = new LinkedHashMap<>();
    private Component component;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Log.i("TestFlowFragment.onComponentAttached([scatter, container, intent]):51");
        component = scatter.parse(ResourceTable.Layout_mifraction_flow, container, false);

        component.findComponentById(ResourceTable.Id_openharmony).setClickedListener(this);
        component.findComponentById(ResourceTable.Id_bug).setClickedListener(this);
        component.findComponentById(ResourceTable.Id_dog).setClickedListener(this);
        icon1 = (Image) component.findComponentById(ResourceTable.Id_icon1);
        icon2 = (Image) component.findComponentById(ResourceTable.Id_icon2);
        icon3 = (Image) component.findComponentById(ResourceTable.Id_icon3);
        textIcon1 = (Text) component.findComponentById(ResourceTable.Id_text_icon1);
        textIcon2 = (Text) component.findComponentById(ResourceTable.Id_text_icon2);
        textIcon3 = (Text) component.findComponentById(ResourceTable.Id_text_icon3);

        resetBottom();
        return component;
    }

    @Override
    protected void setInitialFragmentIfRequired() {
        List<Screen> screens = FractionStack.getInstance(getFractionAbility().getFractionManager()).getCurrentFragmentScreen(this.toString());
        if (screens.size() == 0 && mNavigator.canExecuteCommandImmediately()) {
            Screen screen = getTabScreen(ResourceTable.Id_openharmony);
            mNavigator.switchTo(screen);
        } else {
            mNavigator.reduce(screens);
        }
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        mNavigator = SampleApplication.getNavigator();
    }

    @Override
    protected void onActive() {
        Log.i("TestFlowFragment.onActive([]):70");
        initTabScreenMap();
        super.onActive();
        Log.i("TestFlowFragment.onActive([]):75");
    }

    private void initTabScreenMap() {
        mTabScreenMap.put(ResourceTable.Id_openharmony, new TabScreen(getString(ResourceTable.String_tab_Ohos), getContainerId(), this.toString()));
        mTabScreenMap.put(ResourceTable.Id_bug, new TabScreen(getString(ResourceTable.String_tab_bug), getContainerId(), this.toString()));
        mTabScreenMap.put(ResourceTable.Id_dog, new TabScreen(getString(ResourceTable.String_tab_dog), getContainerId(), this.toString()));
    }


    private void resetBottom() {
        if (nowSelect == ResourceTable.Id_openharmony) {
            icon1.setImageElement(ImageTint.getImagebyColor(getFractionAbility(), ResourceTable.Media_ic_ohos, Color.RED.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon1.setTextColor(Color.RED);
        } else {
            icon1.setImageElement(ImageTint.getImagebyColor(getFractionAbility(), ResourceTable.Media_ic_ohos, Color.BLACK.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon1.setTextColor(Color.BLACK);
        }

        if (nowSelect == ResourceTable.Id_bug) {
            icon2.setImageElement(ImageTint.getImagebyColor(getFractionAbility(), ResourceTable.Media_ic_bug, Color.RED.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon2.setTextColor(Color.RED);
        } else {
            icon2.setImageElement(ImageTint.getImagebyColor(getFractionAbility(), ResourceTable.Media_ic_bug, Color.BLACK.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon2.setTextColor(Color.BLACK);
        }

        if (nowSelect == ResourceTable.Id_dog) {
            icon3.setImageElement(ImageTint.getImagebyColor(getFractionAbility(), ResourceTable.Media_ic_dog, Color.RED.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon3.setTextColor(Color.RED);
        } else {
            icon3.setImageElement(ImageTint.getImagebyColor(getFractionAbility(), ResourceTable.Media_ic_dog, Color.BLACK.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon3.setTextColor(Color.BLACK);
        }
    }

    @Override
    public void onClick(Component component) {
        Screen screen = getTabScreen(component.getId());
        mNavigator.switchTo(screen);
        switch (component.getId()) {
            case ResourceTable.Id_openharmony:
            case ResourceTable.Id_bug:
            case ResourceTable.Id_dog:
                nowSelect = component.getId();
                break;
        }
        resetBottom();
    }

    @Override
    public int getContainerId() {
        return ResourceTable.Id_fragment_container;
    }

    private Screen getTabScreen(int tabId) {
        return mTabScreenMap.get(tabId);
    }

}
